Ext.define('AM.view.EmployeesView', {
		extend: 'Ext.grid.Panel',
		alias : 'widget.employeesView',
		store : 'EmployeesStore',
		border:false,
		columnLines:true,
//		selModel : Ext.create('Ext.selection.CheckboxModel'), 
	    selModel: {
	        injectCheckbox: 0,
	        mode: "SIMPLE"    //"SINGLE"/"SIMPLE"/"MULTI"
//	        checkOnly: true     //只能通过checkbox选择
   		 },
   		selType: "checkboxmodel",
    	dockedItems: [{
        xtype: 'pagingtoolbar',
        dock: 'bottom',
        store : 'EmployeesStore',
        displayInfo: true
    }],
		columns: [	
			        {text: '序号',  xtype: 'rownumberer',width:50,align:'center',tdCls:'background-color: #8EE5EE;'},
			        { text: '姓名',  dataIndex: 'name',  width:80,align:'center'},
			        { text: '科室',dataIndex: 'dept', width:120,align:'center'},
			        { text: '密码', dataIndex: 'password', width:0,align:'center'},
			        { text: '职位', dataIndex: 'position',width:80,align:'center',renderer: function(value){
					        	 if (value === 4) {
										 return '科副主任';
								 }if (value === 1) {
										 return '职员';
								 }
								 if (value === 2) {
										 return '科主任';
								 }if (value === 3) {
										 return '院领导';
								 }if (value === 10) {
										 return '组长';
								 }
								 
								 
								 }
								 },
			        { text: '职称',  dataIndex: 'profession',  width:100,align:'center' },
			        { text: '职称等级',  dataIndex: 'grade',  width:100,align:'center' },
			        { text: '学历',dataIndex: 'education', width:80,align:'center'},
			        { text: '入院时间',dataIndex: 'workage', width:100,align:'center'},
			        { text: '岗位胜任系数',dataIndex: 'postCompetent', width:120,align:'center'},
			        { text: '岗位管理系数',dataIndex: 'postManager', width:120,align:'center'},
			        //{ text: '岗位系数',dataIndex: 'postcoffe', width:100,align:'center'},
			       // { text: '绩效分配系数',dataIndex: 'finaceCoffe', width:100,align:'center'},
			        { text: '是否值班', dataIndex: 'zhiban',width:80,align:'center',hidden:true},
			       
			        { text: '人员ID', dataIndex: 'id',width:80,align:'center',hidden:true},
			        { text: '证件号',dataIndex: 'card', width:200,align:'center'}


			     ],
			   tbar: [
					  { xtype: 'button',id:'tab-5_add', text: '增加',handler: addEmp,icon:'Ext/restful/images/user_add.png'},
					  { xtype: 'button', id:'tab-5_edit',text: '修改',handler: editEmp,icon:'Ext/restful/images/user_edit.png'},
					  { xtype: 'button', id:'tab-5_delete',text: '删除',handler: deleteEmp,icon: 'Ext/restful/images/user_delete.png'},
					  {
					     xtype : "combobox",
					     allowBlank:false,
					     labelAlign :"right",
						 fieldLabel: '选择科室',
						 id:"search_depart",
						 store :Ext.create('AM.store.TargetDepartStore'),
						 displayField: 'name',
						 valueField: 'id',
					     width:300,
					     	  listeners:{   
											afterrender:function(thiz){
						 	                                       if(Number(deptID)>0){
						 	                                       
						                                            thiz.setValue(Number(deptID));
						                                            thiz.setReadOnly(true);
						                                            
											}
											}
								}
					  },
					  { xtype: 'button',id:'tab-5_search', text: '查询',handler: searchemp,icon : 'Ext/restful/images/find.png'
					  }
					]
})
function searchemp() {
		   var grid=this.ownerCt.ownerCt;
			var depart= Ext.getCmp('search_depart').getValue();
	       //此处为加载条件
		  grid.getStore().on('beforeload', function (store, options) {
		  	
		              var new_params = {serchdepartment:depart}
		                Ext.apply(store.proxy.extraParams, new_params);
		       });
	
		grid.getStore().reload();
					
}
function addRole(id) {
		var empID=id;
		 var myform1= new Ext.form.Panel({
		 			bodyPadding: 5,
			        labelWidth : 65,
					   items: [{   
					   		      xtype : "combobox",
								  allowBlank:false,
								  emptyText:'请选择角色',
								  selectOnFocus:true,
								  forceSelection: true,
								  queryMode: 'local',
								  labelAlign :"right",
								  fieldLabel: '角色',
								  name: 'roseID',
								  store :"PowerStore",
								  displayField: 'name',
    							  valueField: 'id'
							},{
							    xtype: 'checkboxgroup',
							    labelAlign :"right",
						        fieldLabel: '数据权限',
						        // Arrange checkboxes into two columns, distributed vertically
						        columns: 3,
						        vertical: true,
						        items: [
						            { boxLabel: '感染管理科 ', name: 'depar', inputValue: '1' },
						            { boxLabel: '疾病预防控制处', name: 'depar', inputValue: '2'},
						            { boxLabel: '医学工程部', name: 'depar', inputValue: '3' },
						            { boxLabel: '计算机室', name: 'depar', inputValue: '4' },
						            { boxLabel: '纪检办公室', name: 'depar', inputValue: '5' },
						            { boxLabel: '宣传教育科', name: 'depar', inputValue: '6' },
						            { boxLabel: '图书馆', name: 'depar', inputValue: '7' },
						            { boxLabel: '党委办公室', name: 'depar', inputValue: '8' },
						            { boxLabel: '院部办公室', name: 'depar', inputValue: '9' },
						            { boxLabel: '医保办', name: 'depar', inputValue: '10' },
						            { boxLabel: '护理部', name: 'depar', inputValue: '11' },
						            { boxLabel: '医务部', name: 'depar', inputValue: '12' },
						            { boxLabel: '科研办公室', name: 'depar', inputValue: '13'},
						            { boxLabel: '离退休办公室', name: 'depar', inputValue: '14'},
						            { boxLabel: '经济管理办公室', name: 'depar', inputValue: '15'}, 
						            { boxLabel: '财务处', name: 'depar', inputValue: '16'},
						            { boxLabel: '干保处', name: 'depar', inputValue: '17'}, 
						            { boxLabel: '工会', name: 'depar', inputValue: '18'},
						            { boxLabel: '门诊办', name: 'depar', inputValue: '19'},
						            { boxLabel: '审计处', name: 'depar', inputValue: '20'},
						            { boxLabel: '人事处', name: 'depar', inputValue: '21'},
						            { boxLabel: '团委', name: 'depar', inputValue: '22'},
						            { boxLabel: '医患办', name: 'depar', inputValue: '23'},
						            { boxLabel: '职工保健室', name: 'depar', inputValue: '24'},
						            { boxLabel: '教育处', name: 'depar', inputValue: '25'}
						              
						        ]
							}]		        	
				        	       

		 })
			var win = new Ext.Window({
				title : "角色授权",
				width : 700,
				height : 400,
				frame:true,
				layout:'fit',
				items : [ myform1
							],
				buttons : [{
							text : "确定",
							handler : function() {
								win.getComponent(0).getForm().submit({
											 url : 'employees_addRole',
											 params :{ empID:empID},
											 success : function() {
												Ext.Msg.alert('提示', '授权成功');
												win.close();
											},
											failure : function() {
												Ext.Msg.alert('错误', '授权失败');
											},
											waitMsg : "请稍后，正在保存。。。。"
	
										});
							}
						}, {
							text : "重置",
							handler : function() {
								win.getComponent(0).getForm().reset();
							}
						}, {
							text : "取消",
							handler : function() {
												win.close();
											
							}
						}]
			});
		win.show();
	}
function addEmp() {
		 var myform1= new Ext.form.Panel({

			        layout : "form",
			        labelWidth : 65,
			        items: [{
					        layout : "column",
					        border : false,
					        	items: [{
							        	columnWidth :.46,
										layout : "form",
										border : false,
										labelWidth : 30,
								   		items: [{
								   				    xtype : "textfield",
								   				    fieldLabel: '姓名',
								   				    labelAlign :"right",
										            name: 'name',
										            allowBlank:false,
										            width:30
										        }]		        	
							        		},{ 
								        	columnWidth :.460,
								        	layout : "form",
								        	 border : false,
											 items:[{
											      		xtype : "textfield",			
											      		labelAlign :"right",
											            fieldLabel: '密码',
											            allowBlank:false,
											            name: 'password',
											            width:20 
											        }]
							        		}]
				        },{
					         layout : "column",
					         border : false,			 
						     items: [{ 
							        	columnWidth :.46,
							        	layout : "form",
							            border : false,
									    items:[{
									      	    xtype : "combobox",
									      	    allowBlank:false,
									      	    emptyText:'请选择科室',
									      	    selectOnFocus:true,
												forceSelection: true,
												editable:false,
												queryMode: 'local',
									      	    labelAlign :"right",
									            fieldLabel: '科室',
									            name: 'department.id',
									            store :"TargetDepartStore",
									            displayField: 'name',
    											valueField: 'id'
									        }]
						        	},{
						        		columnWidth :.46,
								        layout : "form",
								        border : false,
										items:[{
										      	    xtype : "combobox",
										      	    allowBlank:false,
										      	    selectOnFocus:true,
												    forceSelection: true,
												    emptyText:'请选择职称',
												    editable:false,
										      	    labelAlign :"right",
										            fieldLabel: '职称',
										            editable:false,
										            queryMode: 'local',
										            id:'pros1',
										            name:'professional.id',
										            store :"SearchProfessionalStore",
										            displayField:'name',
	    											valueField: 'id'
										            
										        }]
						        	}]
	
					 },{
					 	layout : "column",
					 	 border : false,
				        	items: [{
					        		columnWidth :.46,
						        	layout : "form",
						        	border : false,
								    items:[{
								      	    xtype : "combobox",
								      	    labelAlign :"right",
								            fieldLabel: '学历',
								            forceSelection: true,
											editable:false,
								            emptyText:'请选择学历',
								            name:'education',
								            store:'EducationStore',
								            displayField:'name',
											valueField: 'id'
								        }]
				        		},{
				        			 columnWidth :.46,
				        			 xtype : "datefield",
				        			 emptyText:'请选择日期',
									 fieldLabel: '入职时间',
									 labelAlign :"right",
									 name: 'workage',
									 format:'Y-m-d',
									 forceSelection: true,
									 editable:false
				        		}]
					 	
					 },
//					 	{
//					 	layout : "column",
//					 	border : false,
//				        	items: [{
//						        		columnWidth :.46,
//							        	xtype : "numberfield",
//							      	    labelAlign :"right",
//							            fieldLabel: '岗位系数',
//							            name:'postcoffe',
//							            blankText:"必须为数字",
//								 		minValue: 0
//					        		},{
//					        		    columnWidth :.46,
//					        		    xtype : "numberfield",
//							      	    labelAlign :"right",
//							            fieldLabel: '绩效分配系数',
//							            name:'finaceCoffe',
//							            blankText:"必须为数字",
//								 		minValue: 0
//					        		}]
//					 },
					 	{
			   		 	 layout : "column",
					 	 border : false,
						        	items: [{
							        		columnWidth :.46,
								        	layout : "form",
								        	border : false,
										    items:[{
										      	    xtype : "numberfield",
										      	    labelAlign :"right",
										            fieldLabel: '岗位管理系数',
										            name:'postManager',
										            blankText:"必须为数字",
										    		maxValue: 10,
											 		minValue: 0
										        }]
						        		},{
						        			columnWidth :.3,
								        	layout : "form",
								        	border : false,
								        	items:[{
								        	 	xtype: 'radiogroup',
							        			labelAlign :"right",
										        fieldLabel: '值晚班',
										        columns: 2,
									
										        vertical: true,
										        items: [
										            { boxLabel: '是', name: 'zhiban', inputValue: '2' },
										            { boxLabel: '否', name: 'zhiban', inputValue: '1', checked: true}
										        ]
								        	 }]
						        			
						        		}]
			   		 },{
			   		 	 layout : "column",
					 	 border : false,
					 	 items: [{
					 	 	   columnWidth :.46,
						       layout : "form",
						       border : false,
								    items:[{
									         xtype : "combobox",
											 allowBlank:false,
											 emptyText:'职位',
											 editable:true,
											 selectOnFocus:true,
											 forceSelection: true,
											 editable:false,
											 queryMode: 'local',
											 labelAlign :"right",
											 fieldLabel: '职位',
											 emptyText:'请选择',
											 name: 'position',
											 displayField: 'name',
											 valueField: 'id',
											 store :Ext.create('Ext.data.Store', {
													    fields: ['id', 'name'],
														data : [		
														 		{"id":"1", "name":"职员"},
														        {"id":"4", "name":"科室副主任"},
														        {"id":"2", "name":"科室主任"},
														        {"id":"3", "name":"院领导"},
														        {"id":"10", "name":"组长"}
														      ]
													})
													
								        }]   
					 	 },{
						        columnWidth :.46,
							    layout : "form",
							    border : false,
									    items:[{
									    	 xtype: 'filefield',
						                     labelAlign:'right',
						                     name: 'upload',
						                     msgTarget: 'side',
						                     buttonText:'浏览...',
						                     fieldLabel: '照片'
									        }]      	
						        }]
			   		 },{
			   		 	
			   		 	 layout : "column",
					 	 border : false,
					 	 items: [{
						        columnWidth :.46,
							    layout : "form",
							    border : false,
									    items:[{
									    	 xtype: 'textfield',
						                     labelAlign:'right',
						                     fieldLabel: '身份证号',
											 allowBlank:false,
											 name: 'card',
											 width:20 
						                    
									        }]      	
						        }]
			   		 
			   		 }
			   		 ]
     	
		 })
		    var grid=this.ownerCt.ownerCt;
			var win = new Ext.Window({
				title : "添加员工",
				width : 700,
				height : 350,
//				frame:true,
				layout : "fit",
				items : [ myform1
							],
				buttons : [{
							text : "确定",
							handler : function() {
								win.getComponent(0).getForm().submit({
							    url : 'employees_addEmployees',
								success : function() {
												Ext.Msg.alert('提示', '添加成功');
												win.close();
												grid.getStore().reload();//jobView 为id号
												form.findField('upload').setRawValue(''); 

											},
								 failure : function() {
												Ext.Msg.alert('错误', '添加失败');
											},
											waitMsg : "请稍后，正在保存。。。。"
	
										});
							}
						}, {
							text : "重置",
							handler : function() {
								win.getComponent(0).getForm().reset();
							}
						}, {
							text : "取消",
							handler : function() {
												win.close();
											
							}
						}]
			});
		win.show();
}

	
	function editEmp() {
		  var myform1= new Ext.form.Panel({
			        width : 700, 
			        autoHeight : true,
			        layout : "form",
			        labelWidth : 65,
			        items: [{
					        layout : "column",
					        border : false,
					        	items: [{
							        	columnWidth :.46,
										layout : "form",
										border : false,
										labelWidth : 30,
								   		items: [{
								   				    xtype : "textfield",
								   				    fieldLabel: '姓名',
								   				    allowBlank:false,
								   				    labelAlign :"right",
										            name: 'name',
										            width:30
										        }]		        	
							        		},{ 
								        	columnWidth :.46,
								        	layout : "form",
								        	 border : false,
											 items:[{
											      		xtype : "textfield",			
											      		labelAlign :"right",
											            fieldLabel: '密码',
											            allowBlank:false,
											            name: 'password',
											            width:20 
											        }]
							        		}]
				        },{
					         layout : "column",
					         border : false,			 
						     items: [{ 
							        	columnWidth :.46,
							        	layout : "form",
							        	 border : false,
									      items:[{
									      	    xtype : "combobox",
									      	    allowBlank:false,
									      	    emptyText:'请选择科室',
									      	    selectOnFocus:true,
												forceSelection: true,
											    editable:false,
//												queryMode: 'local',
									      	    labelAlign :"right",
									            fieldLabel: '科室',
									            name: 'dept',
									            store :"TargetDepartStore",
									            displayField: 'name',
    											valueField: 'name'
									        }]
						        	},{
							        	columnWidth :.46,
							        	layout : "form",
							        	border : false,
									    items:[{
									      	    xtype : "combobox",
									      	    allowBlank:false,
									      	    emptyText:'请选择职称',
									      	    selectOnFocus:true,
											    forceSelection: true,
											    editable:false,
											    queryMode: 'local',
									      	    labelAlign :"right",
									            fieldLabel: '职称',
									            name:'profession',
										        store :"SearchProfessionalStore",
									            displayField: 'name',
    											valueField: 'name'
									            
									        }]
						        	
						        	}]
	
					 },{
			   		 	 layout : "column",
					 	 border : false,
						        	items: [{
							        		columnWidth :.46,
								        	layout : "form",
								        	border : false,
										    items:[{
										      	    xtype : "combobox",
										      	    labelAlign :"right",
										            fieldLabel: '学历',
										             editable:true,
										            name:'education',
										            store :'EducationStore',
										            displayField: 'name',
	    											valueField: 'id',
										            width:30
										        }] 
						        		},{						        			
						        	      columnWidth :.46,
							        	  layout : "form",
							        	  border : false,
									       items:[{
										      	     xtype : "datefield",
								        			 emptyText:'请选择日期',
													 fieldLabel: '入职时间',
													 labelAlign :"right",
													 name: 'workage',
													 format:'Y-m-d',
													 forceSelection: true,
													 editable:false
												}]
						        		}]
			   		 },
			   		 	
//			   		 		{
//			   		 	layout : "column",
//					 	border : false,
//				        	items: [{
//						        		columnWidth :.46,
//							        	xtype : "numberfield",
//							      	    labelAlign :"right",
//							            fieldLabel: '岗位系数',
//							            name:'postcoffe',
//							            blankText:"必须为数字",
//								 		minValue: 0
//					        		},{
//					        		    columnWidth :.46,
//					        		    xtype : "numberfield",
//							      	    labelAlign :"right",
//							            fieldLabel: '绩效分配系数',
//							            name:'finaceCoffe',
//							            blankText:"必须为数字",
//								 		minValue: 0
//					        		}]
//			   		 },
			   		 	{
			   		 	layout : "column",
					 	 border : false,
						        	items: [{
							        		columnWidth :.46,
								        	layout : "form",
								        	border : false,
										    items:[{
										      	    xtype : "numberfield",
										      	    labelAlign :"right",
										            fieldLabel: '岗位管理系数',
										            allowBlank:false,
										            name: 'postManager',
										            maxValue: 10,
												    minValue: 0
										        }] 
						        		},{						        			
						        	       columnWidth :.46,
								        	layout : "form",
								        	border : false,
								        	items:[{
								        	 	xtype: 'radiogroup',
							        			labelAlign :"right",
										        fieldLabel: '是否值班',
										        columns: 2,
										        vertical: true,
										        items: [
										            { boxLabel: '是', name: 'zhiban', inputValue: '2' },
										            { boxLabel: '否', name: 'zhiban', inputValue: '1'}
										        ]
								        	 }]
						        		}]
			   		 },{
			   		     layout : "column",
					 	 border : false,
					 	 items: [{
					 	 	   columnWidth :.46,
						       layout : "form",
						       border : false,
								    items:[{
									         xtype : "combobox",
											 allowBlank:false,
											 editable:true,
//											 selectOnFocus:true,
											 labelAlign :"right",
											 fieldLabel: '职位',
											 name: 'position',
											 displayField: 'name',
											 valueField: 'id',
											 store :Ext.create('Ext.data.Store', {
													    fields: ['id', 'name'],
														data : [		
														 		{"id":"1", "name":"科员"},
														        {"id":"4", "name":"副主任"},
														        {"id":"2", "name":"主任"},
														        {"id":"3", "name":"院长"},
														        {"id":"10", "name":"组长"}
														      ]
													})
//											listeners:{
//											         'afterrender': function(combo){
//											         	if(this.value==1){
//											         		 combo.setValue(1);
//											         	}
//											         
//											         }
//											    }
								        }]   
					 			 },{
							        columnWidth :.46,
								    layout : "form",
								    border : false,
										    items:[{
										    	 xtype: 'filefield',
							                     labelAlign:'right',
							                     name: 'upload',
							                     msgTarget: 'side',
							                     buttonText:'浏览...',
							                     fieldLabel: '照片'
										        }]      							        		
						        	}]
			   		 },{
			   		 	layout : "column",
					 	 border : false,
						        	items: [{						        			
						        	    columnWidth :.46,
							        	layout : "form",
							        	border : false,
									      items:[{
									      	    xtype : "textfield",
									      	    labelAlign :"right",
									            fieldLabel: '员工id',
									            name: 'id',
									            hidden : true 
									        }]
						        		}]
			   		 	
			   		 	
			   		 },{
			   		 	
			   		 	 layout : "column",
					 	 border : false,
					 	 items: [{
						        columnWidth :.46,
							    layout : "form",
							    border : false,
									    items:[{
									    	 xtype: 'textfield',
						                     labelAlign:'right',
						                     fieldLabel: '身份证号',
											 allowBlank:false,
											 name: 'card',
											 width:20 
						                    
									        }]      	
						        }]
			   		 
			   		 }]
     	
		 })
			var grid=this.ownerCt.ownerCt;
			var win = new Ext.Window({
				title : "修改员工",
				width : 700,
				height : 350,
				frame:true,
				layout:'fit',
				items : [ myform1
							],
				buttons : [{
							text : "确定",
							handler : function() {
								win.getComponent(0).getForm().submit({
											    url : 'employees_editEmployees',
											    success : function() {
												Ext.Msg.alert('提示', '修改成功');
												win.close();
												grid.getStore().reload();
												grid.getSelectionModel().deselectAll();
											},
											failure : function() {
												Ext.Msg.alert('错误', '添加失败');
											},
											waitMsg : "请稍后，正在保存。。。。"
	
										});
							}
						}, {
							text : "重置",
							handler : function() {
								win.getComponent(0).getForm().reset();
							}
						}, {
							text : "取消",
							handler : function() {
								win.close();
											
							}
						}]
			});
		
		var selections = grid.getSelectionModel().getSelection();
						if(selections.length==0){
							Ext.MessageBox.alert("提示","请选择要编辑的一行！");
						}else if (selections.length > 1) {
							Ext.MessageBox.alert("提示", "请检查您是否选中了多行！");
							return;
						} else{
							var record = selections[0];
						//这里是修改操作的实现部分
							win.getComponent(0).getForm().loadRecord(record);
							win.show();
						}
 }
 
 	function deleteEmp(){
				 var grid=this.ownerCt.ownerCt;
			     var selections = grid.getSelectionModel().getSelection();
					if(selections){
					if(selections.length==0){
							Ext.MessageBox.alert("提示","请检查是否选中某一行！");
						}else if (selections.length > 1) {
							Ext.MessageBox.alert("提示", "请检查您是否选中了多行！");
							return;
						} else{
							Ext.MessageBox.confirm("提示", "确认要删除 ？", function(btn) {
							if (btn == 'yes') {			
							var record = selections[0];
								Ext.Ajax.request({
										url : 'employees_deleteEmployees',
										success : function() {
											Ext.MessageBox.alert("提示",
													"删除" + record.get("name")+ "成功！");
												 grid.getStore().remove(selections );
												 grid.getStore().reload();
										},
										failure : function() {
											Ext.MessageBox.alert("提示", "删除失败！");
										},
										params : {
											id : record.get("id")
										}
									});			
								}
					  	})
                   	} 
 			   
		   }				
	}


